<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>总记录数：{{ total }}</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small" @click="excelFn" v-allow="'import_employee'">导入excel</el-button>
          <el-button type="danger" size="small" @click="exportFn">导出excel</el-button>
          <el-button type="primary" size="small" @click="FormVisible = true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{ prop: 'workNumber' }">
          <el-table-column label="序号" type="index" />
          <el-table-column label="头像">
            <template slot-scope="scope">
              <ImageHolder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template slot-scope="scope"> 形式：{{ format(scope.row.formOfEmployment) }} </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button
                class="aaa"
                type="text"
                size="small"
                @click="$router.push('/employees/detail?id=' + scope.row.id)"
                >查看
              </el-button>
              <el-button type="text" size="small" @click="assignFn(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="curPage"
            :page-sizes="[3, 5, 7, 9]"
            :page-size="100"
            layout="total,
          sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog
        title="新增员工"
        :visible.sync="FormVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <empDialog @close="FormVisible = false" @addSuccess="SussessFn" />
      </el-dialog>
      <el-dialog
        title="分配权限"
        :visible.sync="showDialogRole"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <AssignRole v-if="showDialogRole" :cur-employe-id="curEmployeId" @closeFn="close" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
// import PageTools from '@/components/PageTools'
import empDialog from './components/empDialog.vue'
import AssignRole from './components/assignRole.vue'
import { getEmployeerinfoAPI, delEmployeerAPI } from '@/api/employees.js'
import employeeConstant from '@/api/constant/employees'
const employeeMap = employeeConstant.hireType.reduce((i, item) => {
  i[item.id] = item.value
  return i
}, {})
console.log(employeeMap)
export default {
  components: {
    empDialog,
    AssignRole
  },
  data() {
    return {
      pageParams: {
        page: 1,
        size: 3
      },
      FormVisible: false,
      list: [],
      total: 0,
      curPage: 1,
      showDialogRole: false,
      curEmployeId: ''
    }
  },
  computed: {
    maxPage() {
      return Math.ceil(this.total / this.pageParams.size)
    },
    fullPage() {
      return this.total % this.pageParams.size === 0
    }
  },
  created() {
    this.getEmployeerinfo()
  },
  updated() {},

  methods: {
    assignFn(val) {
      console.log(val)
      this.curEmployeId = val
      this.showDialogRole = true
    },
    close() {
      this.showDialogRole = false
    },
    formatData(list) {
      const map = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      const header = Object.keys(list[0]).map(key => map[key])
      const data = list.map(val => Object.values(val))
      return {
        header,
        data
      }
    },
    async exportFn() {
      await this.getEmployeerinfo()

      const { header, data } = this.formatData(this.list)

      import('@/vendor/Export2Excel').then(excel => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: '员工信息表' // 文件名称
        })
      })
    },
    excelFn() {
      this.$router.push('/import')
    },
    SussessFn() {
      if (this.fullPage) {
        console.log(this.maxPage)
        this.pageParams.page = this.maxPage + 1
        this.getEmployeerinfo()
      } else {
        this.pageParams.page = this.maxPage
        this.getEmployeerinfo()
      }

      this.FormVisible = false
    },
    delFn(id) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delEmployeer(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      console.log(id)
    },
    async delEmployeer(id) {
      const res = await delEmployeerAPI(id)
      if (this.list.length === 1) {
        this.pageParams.page--
        if (this.pageParams.page <= 0) {
          this.pageParams.page = 1
        }
        this.getEmployeerinfo()
      }
      this.getEmployeerinfo()
      console.log(res)
    },
    async getEmployeerinfo() {
      const res = await getEmployeerinfoAPI(this.pageParams)
      this.list = res.data.rows
      this.total = res.data.total
      this.curPage = this.pageParams.page
      console.log(123, res)
    },
    format(val) {
      return employeeMap[val] || '未知'
    },
    handleSizeChange(val) {
      console.log(val)
      this.pageParams.size = val
      this.getEmployeerinfo()
    },
    handleCurrentChange(val) {
      console.log(val)
      this.pageParams.page = val
      this.getEmployeerinfo()
    }
  }
}
</script>
